package api;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;


@WebServlet("/logout")
public class logoutServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //super.doGet(req, resp);

        // 注销操作

        // 先获取会话，看是否有会话对象
        // 注意里面的参数
        //HttpSession session = req.getSession();
        HttpSession session = req.getSession(false);
        if(session == null) {
            resp.setContentType("text/html;charset=utf8");
            resp.getWriter().write("当前未登录");
            return;
        }
        // 有 user 对应的这个会话，直接删除掉，没有的话也没事
        session.removeAttribute("user");
        // 注销之后调转到登陆页面
        resp.sendRedirect("login.html");
    }
}
